package com.hb.trans.mvp.ui.activity


import android.os.Bundle
import android.view.KeyEvent
import android.widget.LinearLayout
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseActivity
import com.basiclib.base.BasePresenter
import com.basiclib.utils.ImmersionBar
import com.hb.trans.R
import com.hb.trans.global.ACTIVITY_WEB
import com.just.agentweb.AgentWeb
import kotlinx.android.synthetic.main.activity_web.*


@Route(path = ACTIVITY_WEB)
class WebActivity : BaseActivity() {
    @Autowired
    @JvmField
    var url: String = ""
    private lateinit var mAgentWeb: AgentWeb

    override fun getContentViewResId(): Int = R.layout.activity_web

    override fun onActivityInject() {
    }

    override fun attachView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this)
            .statusBarColor(R.color.colorPrimary)
            .init()
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        setupWeb()
    }

    override fun getPresenter(): BasePresenter<*, *>? = null
    override fun onPause() {
        mAgentWeb.webLifeCycle.onPause()
        super.onPause()

    }

    override fun onResume() {
        mAgentWeb.webLifeCycle.onResume()
        super.onResume()
    }

    override fun onDestroy() {
        mAgentWeb.webLifeCycle.onDestroy()
        super.onDestroy()
        ImmersionBar.with(this).destroy()
    }

    private fun setupWeb() {
        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(webContainer, LinearLayout.LayoutParams(-1, -1))
            .useDefaultIndicator()
            .createAgentWeb()
            .ready()
            .go(url)
    }

    override fun setListener() {
        webToolbar.backClick()?.subscribe {
            if (!mAgentWeb.back()) {
                finish()
            }
        }
    }

    override fun initData() {

    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {

        return if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            true
        } else super.onKeyDown(keyCode, event)
    }
}
